/*  Lithne Examples
    01. Show own info
    
    This example will show you several ways on how to acquire your own
    info from the local XBee attached. This information is acquired 
    through transmitting AT commands to the XBee.
    By doing so, you can acquire the Serial number (SH & SL), which is 
    essentially the 64-bit address of the XBee, the 16-bit address, the
    Association Status and the PAN ID.

    You can use this information so you don't have to hard-code it into
    your code.
    
    Example created by Remco Magielse, April 2nd 2012.
*/

#include <Lithne.h>  //Include the Lithne Library

void setup()
{
  /*  First, we initiate the connection between the XBee
      and the Arduino at the highest possible rate: 115200.  */
  Lithne.begin( 115200 );
  
  /*  Because the XBee and Arduino need some time to get
      acquinted, we wait untill two seconds have passed.
      This will both give them sufficient time to boot.  */
  while( millis() < 2000 ){ }
  
  /*  The simplest way to acquire your own information is to
      call getMyInfo(). However, this function requests the
      information from the Arduino, but does not return it.
      You have to manualy request the information you want.  */
  Lithne.getMyInfo();
  Serial.print("64-bit HIGH: ");
  Serial.println( Lithne.getMyAddress64().getMsb(), HEX );
  Serial.print("64-bit LOW : ");
  Serial.println( Lithne.getMyAddress64().getLsb(), HEX );
  Serial.print("16-bit ADDR: ");
  Serial.println( Lithne.getMyAddress16(), HEX );
  Serial.print("PAN ID     : ");
  Serial.println( Lithne.getMyPAN(), DEC );
  Serial.print("Assoc. Stat: ");
  Serial.println( Lithne.getMyAssociationStatus(), DEC );
  
  /*  If you do not call getMyInfo() and directly call one
      of the other functions, the Arduino will request the
      information and then wait for a maximum of one second
      for a response. It will only do this when it does not
      yet have this information stored in its memory, e.g.:
      
      Serial.print("16-bit ADDR: ");
      Serial.println( Lithne.getMyAddress16(), HEX );
      */
  
  /*  The other way to acquire information from the XBee is
      by directly requesting an ATCommand and waiting for
      a response. However, this requires you to know what
      commands to send to the XBee, which can be found in
      the datasheet.  
      Here, we tell request the XBee to first send its 
      Serial High data ('S','H') and we tell it to wait
      for maximum of 1000 milliseconds. Similarly, we 
      request the Serial Low data ('S','L').
      */
  uint8_t atSH[]  =  {'S', 'H'};
  uint8_t atSL[]  =  {'S', 'L'};
  long msb    =  Lithne.sendATCommand( atSH, 1000 );
  long lsb    =  Lithne.sendATCommand( atSL, 1000 );
  
  Serial.print("64-bit HIGH: ");
  Serial.println( msb, HEX );
  Serial.print("64-bit LOW : ");
  Serial.println( lsb, HEX );
}

void loop()
{
  
}
